﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.IO;
    using System.Web.Hosting;
    using System.Xml;

    public class Upgrade
    {
        public string DoUpgrade(int PortalID, string StoreInstallPath, int UserId)
        {
            SettingsController controller = new SettingsController();
            string testVersion = SharedFunctions.GetStoreSetting(PortalID, "version", "None");
            string compareVersion = this.getCurrentStoreVersion(StoreInstallPath);
            if (SharedFunctions.VersionCompare(testVersion, compareVersion) != 0)
            {
                if (SharedFunctions.GetStoreSettingBoolean(PortalID, "settings.overwrite"))
                {
                    SharedFunctions.SetStoreSetting(PortalID, "settings.overwrite", "0", "None", true);
                }
                if (SharedFunctions.GetDefaultShipMethod(PortalID) == -1)
                {
                    ShipController controller4 = new ShipController();
                    NB_Store_ShippingMethodInfo objInfo = new NB_Store_ShippingMethodInfo {
                        Disabled = false,
                        MethodDesc = "Standard Shipping",
                        MethodName = "Standard",
                        SortOrder = 1,
                        PortalID = PortalID,
                        ShipMethodID = -1,
                        URLtracker = "",
                        TemplateName = ""
                    };
                    controller4.UpdateObjShippingMethod(objInfo);
                }
                ManagerMenuController controller2 = new ManagerMenuController();
                XmlDataDocument document = new XmlDataDocument();
                ModuleController controller3 = new ModuleController();
                LocaleCollection validLocales = SharedFunctions.GetValidLocales();
                ModuleInfo moduleByDefinition = controller3.GetModuleByDefinition(PortalID, "NB_Store_BackOffice");
                document.Load(HostingEnvironment.MapPath(StoreInstallPath + @"templates\ManagerMenuDefault.xml"));
                controller2.ImportModule(moduleByDefinition.ModuleID, document.SelectSingleNode("content").InnerXml, "1", UserId);
                if (testVersion == "")
                {
                    IEnumerator enumerator;
                    try
                    {
                        enumerator = validLocales.GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            string str6 = Conversions.ToString(enumerator.Current);
                            try
                            {
                                string path = HostingEnvironment.MapPath(StoreInstallPath + @"templates\ManagerMenuDefault_" + str6 + ".xml");
                                if (File.Exists(path))
                                {
                                    document = new XmlDataDocument();
                                    document.Load(path);
                                    controller2.ImportModule(moduleByDefinition.ModuleID, document.SelectSingleNode("content").InnerXml, "1", UserId);
                                }
                            }
                            catch (Exception exception1)
                            {
                                ProjectData.SetProjectError(exception1);
                                Exception exception = exception1;
                                ProjectData.ClearProjectError();
                            }
                        }
                    }
                    finally
                    {
                        if (enumerator is IDisposable)
                        {
                            (enumerator as IDisposable).Dispose();
                        }
                    }
                }
                this.setStoreVersionToCurrent(PortalID, StoreInstallPath);
            }
            return "";
        }

        public string getCurrentStoreVersion(string StoreInstallPath)
        {
            string innerText;
            XmlDataDocument document = new XmlDataDocument();
            document.Load(HostingEnvironment.MapPath(StoreInstallPath + @"templates\ManagerMenuDefault.xml"));
            try
            {
                innerText = document.SelectSingleNode("content").Attributes["version"].InnerText;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                innerText = "00.00.00";
                ProjectData.ClearProjectError();
            }
            return innerText;
        }

        public void setStoreVersionToCurrent(int PortalID, string StoreInstallPath)
        {
            SharedFunctions.SetStoreSetting(PortalID, "version", this.getCurrentStoreVersion(StoreInstallPath), "None", true);
        }
    }
}

